package fdui;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * 合并JS
 * @author hc.tang
 *
 */
public class CombineJS {
	// 存放JS路径的文件
	private static String allJsFile = "C:/Users/hc.tang/git/FDUI/WebContent/allJsUrl.txt";
	// 合并后的文件
	private static String desFile = "C:/Users/hc.tang/git/FDUI/WebContent/src/fdui.all.js";

	/**
	 * 运行完后执行mvn yuicompressor:compress
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		
		String allJsFileStr=allJsFile;
		String desFileStr=desFile;
		
		if(args.length != 2) {
			System.out.println("参数无效");
			System.exit(0);
		}

		allJsFileStr = args[0];
		desFileStr = args[1];
		
		StringBuilder sb = new StringBuilder();
		String jsPath = null;
		
		// 读
		InputStreamReader read = new InputStreamReader(new FileInputStream(allJsFileStr),"UTF-8");
		BufferedReader reader = new BufferedReader(read);
		
		while((jsPath = reader.readLine()) != null) {
			sb.append(getJsContent(jsPath) + "\r\n");
		}
		
		// 写
		OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(desFileStr),"UTF-8");
		BufferedWriter writer=new BufferedWriter(write);   
		
		writer.append(sb.toString());
		
		writer.close();
		read.close();
		System.out.println("合并完毕");
	}
	
	private static String getJsContent(String path) {
		try {
			// 读文件内容
			InputStreamReader read = new InputStreamReader(new FileInputStream(path),"UTF-8");
			BufferedReader reader = new BufferedReader(read);
			StringBuilder sb = new StringBuilder();
			String lineTxt = null;
			
			while((lineTxt = reader.readLine()) != null) {
				sb.append(lineTxt + "\r\n");
			}
			return sb.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
}
